<?php

namespace Budget\BudgetBundle\Controller;

use Budget\BudgetBundle\Entity\Expense;
use Budget\BudgetBundle\Form\ExpenseType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Budget\BudgetBundle\Helper\DateHelper;

class ExpenseController extends MainController
{
    public function indexAction() 
    {
        $em       = $this->getEntityManager();
        $expenses = $em->getRepository('BudgetBudgetBundle:Category')
                       ->getExpensesForFamily($this->getUser()->getFamily());
        
        return $this->render('BudgetBudgetBundle:expense:index.html.twig', array(
            'expenses' => $expenses
        ));
    }
    
    public function newAction(Request $request) 
    {
        $user    = $this->getUser();
        $expense = new Expense();
        $form    = $this->createForm(new ExpenseType($user->getFamily()), $expense);
       
        if ($request->getMethod() == "POST") {
             $form->bind($request);
            
            if ($form->isValid()) {
                $em = $this->getEntityManager();
                $expense->setUser($user);
                $expense->setCreatedBy($user);
                $em->persist($expense);
                $em->flush();
                $this->setPositveMsg('Expense added!');
                return $this->redirect($this->generateUrl('BudgetBudgetBundle_expenses_new'));
            } 
        }
       
        return $this->render('BudgetBudgetBundle:Expense:new.html.twig', array(
            'form' => $form->createView()
        ));
    }
    
    public function editAction(Request $request) 
    {
        $expenseId   = $request->request->get('id');
        $comment     = $request->request->get('comment');
        $categoryId  = $request->request->get('name');
        $date = $request->request->get('date');
        $price       = $request->request->get('price');
        
        $user       = $this->getUser();
        $userFamily = $user->getFamily(); 
        $em         = $this->getEntityManager();
        //$response   = new \stdClass;
        
        $expense = $em->getRepository('BudgetBudgetBundle:Expense')->findByIdForFamily($userFamily, $expenseId);
        if (!$expense) {
            //$response->status = false;
            //return new Response($response);
            return new Response('FALSE');
        }
        
        $category = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($user->getFamily(), $categoryId);
        if (!$category) {
            //$response->status = false;
            //return new Response($response);
            return new Response('FALSE');
        }
        
        $expense->setPrice($price);
        $expense->setComment($comment);
        $expense->setCategory($category);
        $date = \DateTime::createFromFormat(DateHelper::FORMAT_USER, $date);
        $expense->setDate($date);
        $em->persist($expense);
        $em->flush();
        //$response->status = true;
        //return new Response($response);
        return new Response('TRUE');
    }
    
    public function getCategoriesJsonAction() 
    {
        $user       = $this->getUser();
        $userFamily = $user->getFamily(); 
        $em         = $this->getEntityManager();

        $categories = $em->getRepository('BudgetBudgetBundle:Category')->getCategoriesForFamilyToJson($userFamily);

        if ($categories) {
            $response = new Response($categories);
            return $response;
        }
        
        $response = new Response('FALSE');
        return $response;
    }
    
    public function getCategoriesTreeJsonAction() 
    {
        $em         = $this->getEntityManager();
        $categories = $em->getRepository('BudgetBudgetBundle:Category')->getCategoriesForFamilyToJsonTree($this->getUser()->getFamily());

        $response = new JsonResponse();
        $response->setData($categories);
        return $response;
    }
    
    public function deleteAction(Request $request) 
    {
        $id      = $request->request->get('id');
        $user    = $this->getUser();
        $em      = $this->getEntityManager();
        $expense = $em->getRepository('BudgetBudgetBundle:Expense')->findByIdForFamily($user->getFamily(), $id);

        if (!$expense) {
            return new Response('FALSE');
        }
        
        $expense->setDateDeleted(new \DateTime());
        $expense->setDeletedBy($user);
        $em->persist($expense);
        $em->flush();
      
        return new Response('TRUE');
    }
    
}